﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using ArkEC.SEMs.DataAccess.MLM;
using System.Data.Common;
using ArkEC.SEMs.DataAccess;
using ArkEC.CoreCom.Datastruct;

namespace ArkEC.SEMs.Business.MLM
{
    /// <summary>
    /// 语言资源的业务类
    /// </summary>
    public class LanResourceService
    {
        /// <summary>
        /// 导入语言资源
        /// </summary>
        /// <remarks>
        /// 1. 检测数据表是否存在；
        /// 2. 检测excel中的数据格式是否和数据表的格式完全相符；
        /// 3. 循环每行，检测每行的数据是否为空，如果为空，直接跳过，如果不为空，检测KeyCode是否在数据库中存在，如果不存在就调过，并在临时文件中进行记录；
        /// 4. 如果存在，判断LRCode是否在数据库中存在，如果存在，执行更新动作，如果不存在，执行新增动作；
        /// </remarks>
        /// <param name="paramList">资源数据表</param>
        /// <param name="excelPath">导入文件的临时存放路径</param>
        /// <returns></returns>
        public static string Import(Dictionary<string, string> paramList, string excelPath)
        {
            string tableName = paramList["TableName"];

            return tableName;
        }

        /// <summary>
        /// 导出语言资源
        /// </summary>
        /// <remarks>
        /// 将资源数据表中的所有数据全部导出，按照KeyCode和LRCode进行排序
        /// </remarks>
        /// <param name="paramList">资源数据表</param>
        /// <returns></returns>
        public static string Export(Dictionary<string, string> paramList)
        {
            string tableName = paramList["TableName"];

            return "";
        }

        /// <summary>
        /// 获取语言资源的统计数据
        /// </summary>
        /// <returns></returns>
        public static DataTable GetLanResStat()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("LanResTable");

            using (DbConnection conn = ConnFactory.CreateConn())
            {
                #region 构建语言种类列

                DataTable lanTypes = MLMDAFactory.CreateLanTypeDA().GetLanTypes(conn, null);
                foreach (DataRow lanTypeRow in lanTypes.Rows)
                {
                    dt.Columns.Add(ObjectConvertion.DbToString(lanTypeRow["LRCode"]));
                }

                #endregion

                #region 构建语言资源类行

                DataTable lanTables = MLMDAFactory.CreateLanTypeDA().GetLanResTables(conn, null);
                int i = 0;
                foreach (DataRow lanRow in lanTables.Rows)
                {
                    DataRow newRow = dt.NewRow();

                    string tableName = ObjectConvertion.DbToString(lanRow["TableName"]);
                    newRow[0] = tableName;

                    DataTable starLanRes = MLMDAFactory.CreateLanResDA().GetLanResStat(conn, null, tableName);
                    foreach (DataRow lanTypeRow in lanTypes.Rows)
                    { 
                        string lrCode = ObjectConvertion.DbToString(lanTypeRow["LRCode"]);

                        DataRow[] starRows = starLanRes.Select(string.Format("LRCode='{0}'", lrCode));
                        if (starRows.Length > 0)
                        {
                            newRow[lrCode] = starRows[0]["LanResCount"];
                        }
                        else
                        {
                            newRow[lrCode] = "0";
                        }
                    }

                    dt.Rows.Add(newRow);
                    i++;
                }

                #endregion
            }

            return dt;
        }
    }
}
